package com.jzsk.variable.center.zhengxin.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.jzsk.variable.center.common.utils.IpUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

import static cn.hutool.core.net.NetUtil.getLocalhostStr;

/**
 * @author jiangli
 */
@RestController
@Api(tags = "探针API")
public class MonitorController {

    /**
     * 应用名称
     */
    @Value("${spring.application.name}")
    private String appName;

    /**
     * 应用环境
     */
    @Value("${spring.profiles.active}")
    private String appEnv;


    /**
     * 应用启动时间戳
     */
    private String startTimestampStr;

    @GetMapping("/")
    @ApiOperation(value = "根路径", notes = "根路径")
    public String index() {
        return appName + " Server is running. Time: " + DateUtil.now();
    }

    @GetMapping("/monitor")
    @ApiOperation(value = "Docker服务监控接口", notes = "Docker服务监控接口")
    public String monitor(HttpServletRequest request) {
        if (StrUtil.isBlank(startTimestampStr)) {
            startTimestampStr = DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MS_FORMAT);
        }
        return String.format("系统名称: [%s]. 系统环境: [%s]. 服务IP: [%s]. 启动时间: [%s]. 请求IP：[%s].",
                appName, appEnv, getLocalhostStr(), startTimestampStr, IpUtils.getIpAddress(request));
    }

}
